/**
 * 高德API
 * @author 康文程
 * @date 2023-12-14 下午 04:22
 */
class Gaud {
    /**
     * 获取目前天气
     * @param code 城市编码
     * @return {Promise<json>} JSON
     */
    getWeather(code) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "https://restapi.amap.com/v3/weather/weatherInfo?city="+code+"&key=" +
                    "c264f5d39a79cc02595c7ed62218e412",
                method: "GET",
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }

    /**
     * 获取当前城市code
     * @param address code
     * @return {Promise<unknown>} 返回document类型
     */
    getCityCode(address) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: "https://restapi.amap.com/v3/geocode/geo?address=" + address + "&output=XML&key=" +
                    "c264f5d39a79cc02595c7ed62218e412",
                method: "GET",
                success: function (response) {
                    resolve(response);
                },
                error: function (error) {
                    reject(error);
                }
            });
        });
    }

    /**
     * 获取当前位置
     * @return {Promise<string>} String 返回当前位置
     */
    getCurrentLocation() {
        return new Promise((resolve, reject) => {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    let lat = position.coords.latitude;
                    let lng = position.coords.longitude;

                    // 使用高德地图 API 将经纬度转换为地址描述
                    let url = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=' +
                        lng + ',' + lat + '&key=c264f5d39a79cc02595c7ed62218e412';
                    fetch(url)
                        .then(function (response) {
                            return response.json();
                        })
                        .then(function (data) {
                            if (data.status === '1' && data.info === 'OK') {
                                resolve(data.regeocode.formatted_address);
                            } else {
                                reject('获取地址失败：' + data.info);
                            }
                        })
                        .catch(function (error) {
                            reject('获取地址失败：' + error);
                        });
                }, function (error) {
                    reject('获取位置失败：' + error.message);
                });
            } else {
                reject('你的浏览器不支持 Geolocation');
            }
        });
    }

    /**
     * 获取目前经纬度
     * @param address 基础地址信息
     * @return {Promise<string>} String 返回经纬度
     *
     */
    getCurrentLatitudeAndLongitude(address){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: 'https://restapi.amap.com/v3/geocode/geo',
                data: {
                    key: 'c264f5d39a79cc02595c7ed62218e412',
                    address: address,
                    output: 'json'
                },
                method: 'GET',
                success: function (response) {
                    if (response.status === '1' && response.info === 'OK') {
                        let location = response.geocodes[0].location.split(',');
                        let lat = location[1];
                        let lng = location[0];
                        resolve({lat, lng});
                    } else {
                        reject('获取位置失败：' + response.info);
                    }
                },
                error: function (error) {
                    reject('获取位置失败：' + error);
                }
            });
        });
    }

    /**
     * 获取目前城市
     * @return {Promise<string>} String 返回当前位置
     */
    getCity(){
        return new Promise((resolve, reject) => {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    let lat = position.coords.latitude;
                    let lng = position.coords.longitude;

                    // 使用高德地图 API 将经纬度转换为地址描述
                    $.ajax({
                        url: 'https://restapi.amap.com/v3/geocode/regeo',
                        data: {
                            key: 'c264f5d39a79cc02595c7ed62218e412',
                            location: lng + ',' + lat,
                            output: 'json'
                        },
                        method: 'GET',
                        success: function (response) {
                            if (response.status === '1' && response.info === 'OK') {
                                let province = response.regeocode.addressComponent.province;
                                let city = response.regeocode.addressComponent.city;
                                let district = response.regeocode.addressComponent.district;
                                resolve({province, city, district});
                            } else {
                                reject('获取地址失败：' + response.info);
                            }
                        },
                        error: function (error) {
                            reject('获取地址失败：' + error);
                        }
                    });
                }, function (error) {
                    reject('获取位置失败：' + error.message);
                });
            } else {
                reject('你的浏览器不支持 Geolocation');
            }
        });
    }
    /**
     * 根据经纬度获取位置信息
     * @param latitude 纬度
     * @param longitude 经度
     * @return {Promise<string>} String 返回位置信息
     */
    getAddressByLatLng(latitude, longitude) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: 'https://restapi.amap.com/v3/geocode/regeo',
                data: {
                    key: 'c264f5d39a79cc02595c7ed62218e412',
                    location: longitude + ',' + latitude,
                    output: 'json'
                },
                method: 'GET',
                success: function (response) {
                    if (response.status === '1' && response.info === 'OK') {
                        let formatted_address = response.regeocode.formatted_address;
                        resolve(formatted_address);
                    } else {
                        reject('获取位置失败：' + response.info);
                    }
                },
                error: function (error) {
                    reject('获取位置失败：' + error);
                }
            });
        });
    }
}